package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.base.CommonTree;
import com.uwlaser.domain.FactoryModel;
import com.uwlaser.service.FactoryModelService;
import com.uwlaser.service.dto.FactoryModelDto;
import com.uwlaser.service.dto.FactoryModelQueryParam;
import com.uwlaser.service.dto.FactoryPlanHaltProductionDto;
import com.uwlaser.service.dto.FactoryShiftDto;
import com.uwlaser.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author QZK
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工厂建模管理")
@RequestMapping("/api/factoryModel")
public class FactoryModelController {

    private final FactoryModelService factoryModelService;

    @GetMapping
    @Log("查询工厂建模")
    @ApiOperation("查询工厂建模")
    @PreAuthorize("@el.check('factoryModel:list')")
    public ResponseEntity query(FactoryModelQueryParam query, Pageable pageable){
        return new ResponseEntity<>(factoryModelService.queryAll(query,pageable),HttpStatus.OK);
    }

    @ApiOperation("返回全部的设备")
    @GetMapping(value = "/lazy")
    @PreAuthorize("@el.check('factoryModel:list','roles:list')")
    public ResponseEntity<Object> query(@RequestParam Integer pid){
        return new ResponseEntity<>(factoryModelService.getFactoryModel(pid),HttpStatus.OK);
    }

    @ApiOperation("根据设备ID返回所有子节点ID，包含自身ID")
    @GetMapping(value = "/child")
    @PreAuthorize("@el.check('factoryModel:list','roles:list')")
    public ResponseEntity<Object> child(@RequestParam Integer id){
        Set<FactoryModel> factoryModelSet = new HashSet<>();
        List<FactoryModelDto> factoryModelList = factoryModelService.getFactoryModel(id);
        factoryModelSet.add(factoryModelService.getById(id));
        factoryModelSet = factoryModelService.getChildFactoryModels(ConvertUtil.convertList(factoryModelList, FactoryModel.class), factoryModelSet);
        Set<Integer> ids = factoryModelSet.stream().map(FactoryModel::getId).collect(Collectors.toSet());
        return new ResponseEntity<>(ids,HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询工厂建模")
    @ApiOperation("查询工厂建模")
    @PreAuthorize("@el.check('factoryModel:list')")
    public ResponseEntity list(FactoryModelQueryParam query){
        return new ResponseEntity<>(factoryModelService.queryAll(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工厂建模")
    @ApiOperation("新增工厂建模")
    @PreAuthorize("@el.check('factoryModel:add')")
    public ResponseEntity create(@Validated @RequestBody FactoryModelDto resources){
        return new ResponseEntity<>(factoryModelService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工厂建模")
    @ApiOperation("修改工厂建模")
    @PreAuthorize("@el.check('factoryModel:edit')")
    public ResponseEntity update(@Validated @RequestBody FactoryModelDto resources){
        factoryModelService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工厂建模")
    @ApiOperation("删除工厂建模")
    @PreAuthorize("@el.check('factoryModel:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        factoryModelService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/getTree")
    @Log("获取工厂模型树结构")
    @ApiOperation("获取工厂模型树结构")
    @PreAuthorize("@el.check('factoryModel:list')")
    public ResponseEntity getTree() {
        List<CommonTree> list = factoryModelService.getTree();
        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    @GetMapping("/getTreeNotDevice")
    @Log("获取工厂模型树结构排除设备模型")
    @ApiOperation("获取工厂模型树结构排除设备模型")
    @PreAuthorize("@el.check('factoryModel:list')")
    public ResponseEntity getTreeNotDevice() {
        List<CommonTree> list = factoryModelService.getTreeNotDevice();
        return new ResponseEntity<>(list,HttpStatus.OK);
    }


    @GetMapping("/getTreeById")
    @Log("根据ID获取工厂模型树结构")
    @ApiOperation("根据ID获取工厂模型树结构")
    @PreAuthorize("@el.check('factoryModel:list')")
    public ResponseEntity getTreeById(Integer id) {
        List<CommonTree> list = factoryModelService.getTreeById(id);
        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    @PostMapping("/importTree")
    @Log("导入工厂建模")
    @ApiOperation("导入工厂建模")
    @PreAuthorize("@el.check('factoryModel:add')")
    public ResponseEntity importTree(@RequestBody List<CommonTree> resources){
        return new ResponseEntity<>(factoryModelService.importTree(resources),HttpStatus.CREATED);
    }

    /**
     * 获取当前车间月份的日历安排
     */
    @PostMapping("/getCalendar")
    @Log("获取当前车间月份的日历安排")
    @ApiOperation("获取当前车间月份的日历安排")
    public ResponseEntity getCalendar(@Validated @RequestBody FactoryPlanHaltProductionDto resources) throws ParseException {
        return factoryModelService.getCalendar(resources);
    }

    /**
     * 获取当天情况
     */
    @PostMapping("/getCondition")
    @Log("获取当天情况")
    @ApiOperation("获取当天情况")
    public ResponseEntity getCondition(@Validated @RequestBody FactoryShiftDto resources){
        return factoryModelService.getCondition(resources);
    }

    /*
    @Log("导出工厂建模")
    @ApiOperation("导出工厂建模")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('factoryModel:list')")
    public void download(HttpServletResponse response, FactoryModelQueryParam query) throws IOException {
        factoryModelService.download(factoryModelService.queryAll(query), response);
    }*/

}
